package logic

import (
	"context"
	"fmt"
	"my-zero-tiktok/common/constant"
	"my-zero-tiktok/common/xerr"

	"my-zero-tiktok/apps/video/pb/pb"
	"my-zero-tiktok/apps/video/rpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UnlikeVideoLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUnlikeVideoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UnlikeVideoLogic {
	return &UnlikeVideoLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// UnlikeVideo 取消喜欢视频
func (l *UnlikeVideoLogic) UnlikeVideo(in *pb.FavVideoRequest) (*pb.Empty, error) {
	err := l.svcCtx.FavVideoModel.DeleteUserVideo(l.ctx, in.VideoId, in.UserId)
	if err != nil {
		return nil, xerr.New(xerr.VideoUnlikeError)
	}

	favVideoKey := fmt.Sprintf("%s:%d", constant.FavVideoKey, in.VideoId)
	_, _ = l.svcCtx.Redis.IncrbyCtx(l.ctx, favVideoKey, -1)

	userFavVideoKey := fmt.Sprintf("%s:%d", constant.UserFavVideoKey, in.UserId)
	_, _ = l.svcCtx.Redis.LremCtx(l.ctx, userFavVideoKey, 1, fmt.Sprintf("%d", in.VideoId))
	return &pb.Empty{}, nil
}
